/**
 * Copyright (C) 2018-2019
 * All rights reserved, Designed By www.yixiang.co
 * 注意：
 * 本软件为www.yixiang.co开发研制，未经购买不得使用
 * 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 * 一经发现盗用、分享等行为，将追究法律责任，后果自负
 */
package co.yixiang.modules.shop.web.controller;

import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.core.util.StrUtil;
import co.yixiang.annotation.AnonymousAccess;
import co.yixiang.common.api.ApiResult;
import co.yixiang.constant.ShopConstants;
import co.yixiang.enums.RedisKeyEnum;
import co.yixiang.modules.shop.entity.YxInvestIncome;
import co.yixiang.modules.shop.entity.YxSales;
import co.yixiang.modules.shop.entity.YxStoreProduct;
import co.yixiang.modules.shop.entity.YxStoreProductRelation;
import co.yixiang.modules.shop.service.*;
import co.yixiang.modules.shop.service.impl.YxSalesServiceImpl;
import co.yixiang.modules.shop.web.param.YxInvestIncomeQueryParam;
import co.yixiang.modules.shop.web.param.YxStoreProductQueryParam;
import co.yixiang.modules.shop.web.param.YxSystemStoreQueryParam;
import co.yixiang.modules.shop.web.vo.YxInvestIncomeQueryVo;
import co.yixiang.modules.shop.web.vo.YxSystemStoreQueryVo;
import co.yixiang.modules.user.service.YxUserService;
import co.yixiang.utils.FileUtil;
import co.yixiang.utils.RedisUtil;
import co.yixiang.utils.SecurityUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName IndexController
 * @Author hupeng <610796224@qq.com>
 * @Date 2019/10/19
 **/

@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(value = "首页模块", tags = "投资端:首页模块", description = "首页模块")
public class IndexController {

    private final YxInvestIncomeService yxInvestIncomeService;
    private final YxStoreProductService yxStoreProductService;
    private final YxSalesServiceImpl yxSalesServiceImpl;
    private final YxInvestCartService yxInvestCartService;


    //   @Cacheable(cacheNames = ShopConstants.YSHOP_REDIS_INDEX_KEY)
    @ApiOperation(value = "首页数据", notes = "首页数据")
    @GetMapping(value = "/invest/index")
    public ApiResult<Map<String, Object>> index(String storeId) {
        if(storeId == null || storeId.equals("")){
            storeId = "1";
        }
        Map<String, Object> map = new LinkedHashMap<>();
        //销售信息
        int uid = SecurityUtils.getUserId().intValue();
        map.put("salesInfo", yxSalesServiceImpl.getById(uid));
        //购物车数量
        map.put("cartCount", yxInvestCartService.getUserProductCartList(uid,"",0));

        //获取投资收益
        YxInvestIncomeQueryParam yxInvestIncomeQueryParam = new YxInvestIncomeQueryParam();
        yxInvestIncomeQueryParam.setPage(1);
        yxInvestIncomeQueryParam.setLimit(10);
        List<YxInvestIncomeQueryVo> incomeRanking = yxInvestIncomeService.getList(yxInvestIncomeQueryParam);
        map.put("incomeRanking", incomeRanking);
        //销售量排行榜
        YxStoreProductQueryParam yxStoreProductQueryParam = new YxStoreProductQueryParam();
        yxStoreProductQueryParam.setPage(1);
        yxStoreProductQueryParam.setLimit(10);
        yxStoreProductQueryParam.setStoreId(storeId);
        List<YxStoreProduct> saleRanking = yxStoreProductService.saleRanking(yxStoreProductQueryParam);
        map.put("saleRanking", saleRanking);


        //获取商品数据
       // List<YxStoreProduct> bestList = yxStoreProductService.getListProduct(yxStoreProductQueryParam);
        List<YxStoreProduct> bestList = yxStoreProductService.getListProductRandom(storeId,8);

        map.put("bestList", bestList);
        return ApiResult.ok(map);
    }


    @ApiOperation(value = "换一批", notes = "换一批")
    @GetMapping(value = "/invest/exchange")
    public ApiResult<Map<String, Object>> exchange(String storeId) {
        Map<String, Object> map = new LinkedHashMap<>();
        if(storeId == null || storeId.equals("")){
            storeId = "1";
        }

        //获取商品数据
        List<YxStoreProduct> bestList = yxStoreProductService.getListProductRandom(storeId,8);
        map.put("bestList", bestList);


        return ApiResult.ok(map);
    }


}
